﻿Imports OMANE.Common.UI
Imports OMANE.Common
Imports System.Windows.Input
Imports System.ComponentModel
Imports PTIC_Util
Imports Microsoft.Practices.Prism.Commands

Namespace ViewModels
    Public Class ViewModelCalculsDetailles
        Inherits ViewModelCommand
        Implements IViewModelTabSection


        Sub Initialize()
            Dim rnd As New Random(Now.Millisecond)

            _dataset = New DataSet()
            Try
                _sourceTable = Locator.ModelStatic.ListeTraitements.TraitMoteurMethode1.ScenarioCentral.resultats.CalculsDetailles.Copy()
                _sourceTable.TableName = "SourceTable"
                If Not _sourceTable.Columns.Contains("SourceID") Then
                    _sourceTable.Columns.Add("SourceID", GetType(Integer))
                    _sourceTable.Columns.Add("Weight", GetType(Double))
                    For i As Integer = 0 To _sourceTable.Rows.Count - 1
                        _sourceTable.Rows(i)("SourceID") = i
                        _sourceTable.Rows(i)("Weight") = CDbl(1)
                    Next
                    _sourceTable.Columns("SourceID").AllowDBNull = False
                    _sourceTable.Columns("SourceID").Unique = True
                End If
                _sourceTable.PrimaryKey = New DataColumn() {_sourceTable.Columns("SourceID")}
                _dataset.Tables.Add(_sourceTable)

                _groupTable = Locator.ModelStatic.ListeTraitements.TraitMoteurMethode1.ScenarioCentral.resultats.CalculsDetailles.Copy()
                '_groupTable.Rows.Clear()
                _groupTable.TableName = "GroupTable"
                _sourceTable.DataSet.Tables.Add(_groupTable)
                _groupTable.Columns.Add("GroupID", GetType(Integer))
                _groupTable.Columns("GroupID").AutoIncrement = True
                _groupTable.Columns("GroupID").AutoIncrementSeed = 0
                _groupTable.Columns("GroupID").AutoIncrementStep = 1
                _groupTable.Columns.Add("SumOfWeight", GetType(Double))
                For i As Integer = 0 To _sourceTable.Rows.Count - 1
                    _groupTable.Rows(i)("GroupID") = i
                    _groupTable.Rows(i)("SumOfWeight") = CDbl(1)
                Next
                _groupTable.Columns("GroupID").AllowDBNull = False
                _groupTable.Columns("GroupID").Unique = True
                _groupTable.PrimaryKey = New DataColumn() {_groupTable.Columns("GroupID")}

                _linkTable = New DataTable()
                _sourceTable.DataSet.Tables.Add(_linkTable)
                _linkTable.Columns.Add("GroupID", GetType(Integer))
                _linkTable.Columns.Add("SourceID", GetType(Integer))
                _linkTable.Columns.Add("SpecificWeight", GetType(Double))
                _linkTable.PrimaryKey = New DataColumn() _
                    {_linkTable.Columns("GroupID"), _
                     _linkTable.Columns("SourceID")}
                For i As Integer = 0 To _sourceTable.Rows.Count - 1
                    _linkTable.Rows.Add(i, i, CDbl(1))
                Next
            Catch ex As Exception
                _sourceTable = New DataTable()
                _dataset.Tables.Add(_sourceTable)
                _sourceTable.Columns.Add("SourceID", GetType(Integer))
                _sourceTable.Columns.Add("Weight", GetType(Double))
                For i As Integer = 0 To rnd.Next(4, 10)
                    _sourceTable.Columns.Add("Column_" & i, GetType(Double))
                Next
                _sourceTable.PrimaryKey = New DataColumn() {_sourceTable.Columns("SourceID")}
                For i As Integer = 0 To rnd.Next(25, 35)
                    Dim r = _sourceTable.NewRow()
                    For Each col As DataColumn In _sourceTable.Columns
                        If col.Ordinal = 0 Then
                            r(col) = i
                        ElseIf col.Ordinal = 1 Then
                            r(col) = Math.Round(CDbl(100) * rnd.NextDouble(), 2)
                        Else
                            r(col) = Math.Round(rnd.NextDouble(), 2)
                        End If
                    Next
                    _sourceTable.Rows.Add(r)
                Next

                _groupTable = New DataTable("DetailViewGroupTable")
                _sourceTable.DataSet.Tables.Add(_groupTable)
                _groupTable.Columns.Add("GroupID", GetType(Integer))
                _groupTable.Columns("GroupID").AutoIncrement = True
                _groupTable.Columns("GroupID").AutoIncrementSeed = 0
                _groupTable.Columns("GroupID").AutoIncrementStep = 1
                _groupTable.Columns.Add("SumOfWeight", GetType(Double))
                _groupTable.PrimaryKey = New DataColumn() {_groupTable.Columns("GroupID")}

                _linkTable = New DataTable()
                _sourceTable.DataSet.Tables.Add(_linkTable)
                _linkTable.Columns.Add("GroupID", GetType(Integer))
                _linkTable.Columns.Add("SourceID", GetType(Integer))
                _linkTable.Columns.Add("SpecificWeight", GetType(Double))
                _linkTable.PrimaryKey = New DataColumn() _
                    {_linkTable.Columns("GroupID"), _
                     _linkTable.Columns("SourceID")}
            End Try



            Dim sourceToLink As DataRelation
            sourceToLink = New DataRelation( _
                "SourceToLink", _
                _sourceTable.Columns("SourceID"), _
                _linkTable.Columns("SourceID"))
            _dataset.Relations.Add(sourceToLink)

            Dim groupToLink As DataRelation
            groupToLink = New DataRelation( _
                "GroupToLink", _
                _groupTable.Columns("GroupID"), _
                _linkTable.Columns("GroupID"))
            _dataset.Relations.Add(groupToLink)

            _ResultatsScenarioCentral = _sourceTable
        End Sub
        Private _Model As ModelTauxGarantis
        Public Sub New(ByVal Model As ModelTauxGarantis, ByVal DisplayName As String, ByVal Command As ICommand)
            MyBase.DisplayName = DisplayName
            _Model = Model
            Title = "CALCULS DETAILLES"
            Initialize()
        End Sub
        Public Sub New()
            If Not Locator.ModelStatic Is Nothing Then
                Initialize()
            End If
        End Sub

        Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
        Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable

        Dim _sourceTable As DataTable
        Dim _linkTable As DataTable
        Dim _groupTable As DataTable
        Dim _dataset As DataSet

        Private _GroupCommand As New DelegateCommand(Of Object)(AddressOf GroupExecute, AddressOf GroupCanExecute)
        Public ReadOnly Property GroupCommand As DelegateCommand(Of Object)
            Get
                Return _GroupCommand
            End Get
        End Property
        Private Sub GroupExecute(ByVal parameter As Object)

        End Sub
        Private Function GroupCanExecute(ByVal parameter As Object) As Boolean
            Return True
        End Function

        Private _UnGroupCommand As New DelegateCommand(Of Object)(AddressOf UnGroupExecute, AddressOf UnGroupCanExecute)
        Public ReadOnly Property UnGroupCommand As DelegateCommand(Of Object)
            Get
                Return _UnGroupCommand
            End Get
        End Property
        Private Sub UnGroupExecute(ByVal parameter As Object)

        End Sub
        Private Function UnGroupCanExecute(ByVal parameter As Object) As Boolean
            Return True
        End Function

        Private _ViewTableCommand As New DelegateCommand(Of Object)(AddressOf ViewTableExecute, AddressOf ViewTableCanExecute)
        Public ReadOnly Property ViewTableCommand As DelegateCommand(Of Object)
            Get
                Return _ViewTableCommand
            End Get
        End Property
        Private Sub ViewTableExecute(ByVal parameter As Object)
            If parameter = "Link" Then
                ResultatsScenarioCentral = _linkTable
            End If
            If parameter = "Original" Then
                ResultatsScenarioCentral = _sourceTable
            End If
            If parameter = "Group" Then
                ResultatsScenarioCentral = _groupTable
            End If
        End Sub
        Private Function ViewTableCanExecute(ByVal parameter As Object) As Boolean
            Return True
        End Function


        Private _ResultatsScenarioCentral As DataTable
        Public Property ResultatsScenarioCentral As DataTable
            Get
                Return _ResultatsScenarioCentral
            End Get
            Set(ByVal value As DataTable)
                OnPropertyChanging("ResultatsScenarioCentral")
                _ResultatsScenarioCentral = value
                OnPropertyChanged("ResultatsScenarioCentral")
            End Set
        End Property

        Public ReadOnly Property OriginalTable As DataTable
            Get
                Return _sourceTable
            End Get
        End Property
        Public ReadOnly Property GroupTable As DataTable
            Get
                Return _groupTable
            End Get
        End Property
        Public ReadOnly Property LinkTable As DataTable
            Get
                Return _linkTable
            End Get
        End Property

        Public Sub RefreshFinalTable()
            OnPropertyChanged("FinalTable")
        End Sub

        Private _finalTable As DataTable
        Public ReadOnly Property FinalTable As DataTable
            Get
                'Dim q = From e In LinkTable Select CInt(e("SourceID")) Distinct
                'Dim q2 = From e In OriginalTable Select CInt(e("SourceID")) Distinct
                'Dim q3 = Enumerable.Except(Of Int32)(q2, q)

                'Dim result As New DataTable
                'result = GroupTable.Copy()
                'For Each e In q3
                '    result.ImportRow(OriginalTable.Rows.Find(e))
                'Next
                'Return result
                Return GroupTable
            End Get
        End Property

        'Public ReadOnly Property ResultatsScenarioCentral As DataTable
        '    Get
        '        Return _sourceTable '_Model.Univers.listeTraitements.TraitMoteurMethode1.ScenarioCentral.resultats
        '    End Get
        'End Property

        Private _SelectedOriginalRow As IList
        Public Property SelectedOriginalRow As IList
            Get
                Return _SelectedOriginalRow
            End Get
            Set(ByVal value As IList)
                OnPropertyChanging("SelectedOriginalRow")
                Dim args As New PropertyChangeEventArgs(Of IList)(_SelectedOriginalRow, value)
                OnSelectedOriginalRowChange(args)
                _SelectedOriginalRow = args.NewValue
                OnPropertyChanged("SelectedOriginalRow")
            End Set
        End Property
        Protected Overridable Sub OnSelectedOriginalRowChange(ByVal args As PropertyChangeEventArgs(Of IList))
            RaiseEvent SelectedOriginalRowChange(Me, args)
        End Sub
        Public Event SelectedOriginalRowChange As PropertyChangeEventHandler(Of IList)

    End Class
End Namespace
